package com.g7.dsp.test.swing.processbar;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

//SwingUtilities.invokeLater()在把可运行的对象放入队列后就返回，而SwingUtilities.invokeAndWait()一直等待知道已启动了可运行的run方法才返回。
//如果一个操作在另外一个操作执行之前必须从一个组件获得信息，则应使用SwingUtilities.invokeAndWait()方法。 
public class SwingThreadTest3 extends JFrame {
	private static final long serialVersionUID = 1L;
	private static final String STR = "Completed : ";
	private JProgressBar progressBar = new JProgressBar();
	private JTextField text = new JTextField(10);
	private JButton start = new JButton("Start");
	private JButton end = new JButton("End");
	private boolean flag = false;
	private int count = 0;

	private GoThread t = null;

	private Runnable run = null;// 更新组件的线程

	public SwingThreadTest3() {
		this.setLayout(new FlowLayout());
		add(progressBar);
		text.setEditable(false);
		add(text);
		add(start);
		add(end);
		start.addActionListener(new Start());
		end.addActionListener(new End());

		run = new Runnable() {// 实例化更新组件的线程
			public void run() {
				progressBar.setValue(count);
				text.setText(STR + String.valueOf(count) + "%");
			}
		};
	}

	private void go() {
		while (count < 100) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (flag) {
				count++;
				SwingUtilities.invokeLater(run);// 将对象排到事件派发线程的队列中
			}
		}
	}

	private class Start implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			flag = true;
			if (t == null) {
				t = new GoThread();
				t.start();
			}
		}
	}

	class GoThread extends Thread {
		public void run() {
			// do something...
			go();
		}
	}

	private class End implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			flag = false;
		}
	}

	public static void main(String[] args) {
		SwingThreadTest3 fg = new SwingThreadTest3();
		fg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fg.setSize(300, 100);
		fg.setVisible(true);
	}
}